package com.wang.chatbot.api.job;

import com.wang.chatbot.api.domain.ai.service.IAiApi;
import com.wang.chatbot.api.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import com.wang.chatbot.api.domain.zsxq.model.vo.Comments;
import com.wang.chatbot.api.domain.zsxq.service.Imp.ZsxqApi;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.*;
import java.util.logging.Logger;

/**
 * @author Your Name
 * @date 2025/11/8 17:17
 * <Description of the class>
 */
@EnableScheduling
@Configuration
public class ChatBotSchedule implements Runnable{
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(ChatBotSchedule.class);
    private Logger logger = Logger.getLogger(ChatBotSchedule.class.getName());
    @Value("${Ai.apiKey}")
    private String apiKey;
    @Value("${Ai.apiUrl}")
    private String apiUrl;
    @Value("${chatbot.cookie}")
    private String cookie;
    @Resource
    private ZsxqApi zsxqApi;
    @Resource
    private IAiApi aiApi;
    private static Map<String,Boolean> commentedList = new HashMap<>();
    @Scheduled(cron = "0/30 * * * * ?")

    @Override
    public void  run(){
        logger.info("开始执行定时任务·····");
        if (new Random().nextBoolean()){
            logger.info("随机打烊中·····");
        }
        try{
            UnAnsweredQuestionsAggregates unAnsweredQuestionsAggregates = zsxqApi.queryUnAnsweredQuestionsTopicId(cookie);
            if(!unAnsweredQuestionsAggregates.isSucceeded()){
                logger.info("获取评论失败");
                throw new RuntimeException("获取评论失败");
            }
            long ownerId = unAnsweredQuestionsAggregates.getResp_data().getComments().get(0).getOwner().getUser_id();
            List<Comments> comments = unAnsweredQuestionsAggregates.getResp_data().getComments();
            if (comments==null){
                logger.info("本次检索没有搜索到问题");
                return;
            }
            for (Comments comment : comments) {
                if(comment.getOwner().getUser_id() == ownerId){
                    // 判断评论是否是作者回复的评论
                    logger.info("这是作者的评论，我不能回复");
                    continue;
                }
                if(commentedList.containsKey(comment.getComment_id())){
                    //这条已经评论过了
                    logger.info("已经回复过该条评论");
                    continue;
                }
                String question = comment.getText().trim();
                String answer = aiApi.doChat(question);
                boolean suc = zsxqApi.answer(cookie, answer);
                if (!suc){
                    //如果当前评论回答失败，没关系直接进入下一个评论，一分钟之后会进行补偿重新回答
                    logger.info("回答失败");
                    continue;
                }
            }
        }catch (Exception e){
                logger.info("回答失败");
        }
    }

}
